package com.rfid.emergencywarehouse.presenter;

import com.rfid.base.base.BasePresenter;
import com.rfid.emergencywarehouse.entity.NameValueBean;
import com.rfid.emergencywarehouse.net.NetApi;
import com.rfid.emergencywarehouse.net.entity.CreateListingRes;
import com.rfid.emergencywarehouse.net.entity.RackRes;
import com.rfid.emergencywarehouse.net.entity.WareHouseareaRes;
import com.rfid.emergencywarehouse.net.entity.WorkAreaRes;
import com.rfid.emergencywarehouse.view.ICreateListingView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CreateListingPresenter extends ReadTagPresenter<ICreateListingView> {


    public void getKWAndYJG() {
        mExcutor.execute(new Runnable() {
            @Override
            public void run() {
                List<NameValueBean> beans = new ArrayList<>();
                try {
                    WorkAreaRes res = NetApi.INSTANCE.getWorkArea();
                    for (WorkAreaRes.DataBean bean : res.getData()) {
                        NameValueBean b = new NameValueBean();
                        b.setName(bean.getDisplayName());
                        b.setValue(bean.getId());
                        beans.add(b);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
//                try {
//                    RackRes res = NetApi.INSTANCE.getRackList();
//                    for (RackRes.DataBean bean : res.getData()) {
//                        NameValueBean b = new NameValueBean();
//                        b.setName(bean.getDisplayName());
//                        b.setValue(bean.getId());
//                        beans.add(b);
//                    }
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (mView != null) {
                            mView.onGetKWYJGList(beans);
                        }
                    }
                });
            }
        });
    }

    public void createListing(String id,List<String> barcodes) {
        mExcutor.execute(new Runnable() {
            @Override
            public void run() {
                boolean s = false;
                String msg = "";
                try {
                    CreateListingRes res = NetApi.INSTANCE.createListing(id,barcodes);
                    if (res.getCode() == 0) {
                        s = true;
                    } else {
                        msg = res.getMsg();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    s = false;
                    msg = e.getMessage();
                }
                boolean finalS = s;
                String finalMsg = msg;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (mView != null) {
                            mView.onCreateListing(finalS, finalMsg);
                        }
                    }
                });
            }
        });
    }
}
